package kgame5.k3d.core.obj3d.node3d.animator.channel {
import kgame5.k3d.core.math.Vertex3D;
import kgame5.k3d.core.obj3d.node3d.animator.Animator3D;

/**
	 * @author Tim Knip
	 */ 
	 //TODO 这个还没弄好
	public class MorphChannel3D extends AbstractChannel3D
	{
		/**
		 * Constructor.
		 * 
		 * @param	target
		 * @param	name
		 */ 
		public function MorphChannel3D(target:Animator3D, name:String=null)
		{
			super(target, name); 
		}	
		
		/**
		 * Updates this channel.
		 * 
		 * @param	keyframe
		 * @param	target
		 */ 
		override public function updateToFrame(keyframe:uint):void
		{
			super.updateToFrame(keyframe);	
			
			var curOutput:Vector.<Vertex3D> = currentKeyFrame.output;
			
//			if(target.gi==null || target.gi.aVB_p==null){
//				return;
//			}
				
//			if(curOutput.length != target.gi.aVB_p.length){
//				return;
//			}
			//TODO 这个没弄好
			for(var i:int = 0; i < target.gi.aVertex.length; i++)
			{
				var v:Vertex3D = target.gi.aVertex[i];
				var w:Vertex3D = curOutput[i];
				
				v.x = w.x;
				v.y = w.y;
				v.z = w.z;
			}
		}
		
		/**
		 * Updates this channel by time.
		 * 
		 * @param	time	Value between 0 and 1 indicating position between #startTime and #endTime.
		 */ 
		public override function updateToTime(time:Number):void
		{
			super.updateToTime(time);
			
			var curOutput:Vector.<Vertex3D> = currentKeyFrame.output;
			var nxtOutput:Vector.<Vertex3D> = nextKeyFrame ? nextKeyFrame.output : null;
			
//			if(target.gi==null || target.gi.aVB_p==null){
//				return;
//			}
				
//			if(curOutput.length != target.gi.aVB_p.length){
//				return;
//			}
				
			//----------------
			for(var i:int = 0; i < target.gi.aVertex.length; i++)
			{
				var u:Vertex3D = target.gi.aVertex[i];
				var v:Vertex3D = curOutput[i];
				var w:Vertex3D = nxtOutput ? nxtOutput[i] : curOutput[i];
				
				u.x = v.x + frameAlpha * (w.x - v.x);
				u.y = v.y + frameAlpha * (w.y - v.y);
				u.z = v.z + frameAlpha * (w.z - v.z);
			}
			
		}
	}
}
